﻿using RazorEngine;
using RazorEngine.Templating;
using RuoVea.ExCrypt;
using System.Collections.Generic;
using System.Linq;

namespace RuoVea.ExViewEngine;

/// <summary>
/// 
/// </summary>
public class ViewEngine : IViewEngine
{
    /// <summary>
    /// 生成渲染模型
    /// </summary>
    /// <param name="content">模板内容</param>
    /// <param name="model">模型数据</param>
    /// <param name="usings">引用</param>
    /// <returns>模型字符串</returns>
    public string RunCompile(string content, object model, List<string>? usings)
    {
        if (usings == null || usings.Count() == 0)
            return Engine.Razor.RunCompile(content, Md5Crypt.Encrypt(content), null, model);
        else {
            string contents = "";
            foreach (string item in usings) 
                contents += item + "\n";
            return Engine.Razor.RunCompile((contents+content), Md5Crypt.Encrypt(content), null, model);
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="content"></param>
    /// <param name="model"></param>
    /// <param name="usings"></param>
    /// <returns></returns>
    public string RunCompile<T>(string content, object model, List<string>? usings)
    {
        if (usings == null || usings.Count() == 0)
            return Engine.Razor.RunCompile(content, Md5Crypt.Encrypt(content), typeof(T), model);
        else
        {
            string contents = "";
            foreach (string item in usings)
                contents += item + "\n";
            return Engine.Razor.RunCompile((contents + content), Md5Crypt.Encrypt(content), typeof(T), model);
        }
    }
}